package com.starbaba.system.menu.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.hibernate.Strategy;
import com.starbaba.core.web.constant.WebConstants;
import com.starbaba.system.menu.model.MenuVO;
import com.starbaba.system.roleofmenu.model.RoleOfMenuVO;
import com.starbaba.system.user.model.UserVO;
import com.starbaba.system.usergroupcontrol.model.UsergroupControlVO;
import com.starbaba.system.userofgroup.model.UserOfGroupVO;

/**
 * 根据菜单ID获取所有能访问的子菜单ID
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings({"serial","unchecked","rawtypes"})
public class QueryIsTrueButtonStrategy implements Strategy {

	private UserVO userVO;

	private String url;


	public QueryIsTrueButtonStrategy(UserVO userVO, String url) {
		this.userVO = userVO;
		this.url = url;
	}

	@Override
	public Object process() throws Exception {
		Boolean flag = false ;
		StringBuffer sb = new StringBuffer();
		HibernateDAO controlDao = DAOFactory.getInstance().createHibernateDAO(MenuVO.class);
		HibernateDAO roleOfMenuDao= DAOFactory.getInstance().createHibernateDAO(RoleOfMenuVO.class);
		HibernateDAO userOfGroupDao = DAOFactory.getInstance().createHibernateDAO(UserOfGroupVO.class);
		HibernateDAO usergroupControlDao = DAOFactory.getInstance().createHibernateDAO(UsergroupControlVO.class);
		if (userVO.isSuperUser()) {
			flag = true;
			return flag;
		}
		url = "%"+ url+"%";
		String querybuttonsql = " from MenuVO  where status = ? and type = ?  and url like ? " ;
		List result = controlDao.findByHQL(querybuttonsql,new Object[]{ WebConstants.STATUS_VALID, WebConstants.MENUTYPE_BUTTON, url});	

		if(result==null||result.size()==0){
			flag = true;
		}else {
			//角色id关联控制点查看按钮的url是否有权限
			
			MenuVO vo  = (MenuVO)result.get(0);
			List roles = userVO.getRoleIdList();
			sb.append(" from ");
			sb.append(RoleOfMenuVO.class.getName()).append(" as rm ");
			sb.append(" where ");
			sb.append(" rm.controlid= ?  ");
			sb.append(" and rm.roleid in ('");
			if (!CollectionUtils.isEmpty(roles)) {
				List tempList = new ArrayList(roles);
				sb.append(StringUtils.join(tempList, "','"));
				tempList.clear();
			} 
			sb.append("' ) ");
			List num = roleOfMenuDao.findByHQL(sb.toString(), new Object[]{vo.getControlid()});
			if(num.size()>0&&num!=null){
				flag = true;
				return flag;
			}
			//用户组关联菜单控制点
			StringBuffer usersql = new StringBuffer();	
			usersql.append(" from ");
			usersql.append(UserOfGroupVO.class.getName()).append(" as ug ");
			usersql.append(" where ug.userid = ? "  );
			List<UserOfGroupVO>  userGrouplist= (List<UserOfGroupVO>) userOfGroupDao.findByHQL(usersql.toString(), new Object[]{userVO.getUserid()});
			if(userGrouplist !=null && userGrouplist.size()>0){
				StringBuffer groupcontrol = new StringBuffer();	
				groupcontrol.append(" from ");
				groupcontrol.append(UsergroupControlVO.class.getName()).append(" as ugc ");
				groupcontrol.append(" where ugc.groupid = ? and ugc.controlid = ?"  );
				
				for(UserOfGroupVO userOfGroup : userGrouplist){
					Long groupid = userOfGroup.getGroupid();
					List controllist= usergroupControlDao.findByHQL(groupcontrol.toString(), new Object[]{groupid,vo.getControlid()});
					if(controllist != null && controllist.size()>0){
						flag = true;
						return flag;
					}
				}
			}
		}
		return flag;
	}

	/**
	 * @return userVO
	 */
	
	public UserVO getUserVO() {
		return userVO;
	}

	/**
	 * @param userVO the userVO to set
	 */
	public void setUserVO(UserVO userVO) {
		this.userVO = userVO;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	



}
